package com.prp.gwt.client.user.friend;

import java.util.Set;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.prp.gwt.client.ClientUtility;
import com.prp.gwt.client.Host;
import com.prp.gwt.client.vo.User;
import com.prp.gwt.client.vo.UserList;
import com.prp.gwt.client.widgets.MessageDialogBox;
import com.prp.gwt.client.widgets.user.friend.FriendItem;
import com.prp.gwt.client.widgets.user.friend.FriendListPanel;
import com.prp.gwt.client.widgets.user.friend.InvitationListPanel;
import com.prp.gwt.client.widgets.user.friend.MyRecommendationList;
import com.prp.gwt.client.widgets.user.friend.RecommendPanel;
import com.prp.gwt.client.widgets.user.php.FriendsPreviewPanel;

public class FriendListener {

	private static FriendListener instance;
	private FriendServiceAsync service = GWT.create(FriendService.class);
	public UserList friendList;
	private boolean dirty;

	public static FriendListener getInstance() {
		if (instance == null)
			instance = new FriendListener();
		return instance;
	}

	public void getFriendList(final int id) {
		if (instance.friendList == null || id != -1 || dirty)
			refreshFriendList(id);
		else {
			FriendListPanel.getInstance().load(instance.friendList, "我的邻友", true);
			FriendsPreviewPanel.getInstance().load(instance.friendList);
			for (int i = 0; i < instance.friendList.size(); i++) {
				User user = instance.friendList.get(i);
				RecommendPanel.getInstance().addSuggest(
						user.getUsername() + " \"" + user.getEmail() + "\"");
			}
		}

	}

	public void refreshFriendList(final int id) {
		service.getFriendList(id, new AsyncCallback<UserList>() {
			@Override
			public void onFailure(Throwable caught) {
				ClientUtility.handleException(caught);
			}

			@Override
			public void onSuccess(UserList result) {
				if (result == null)
					Window.Location.replace(Host.getPAGE_NOT_FOUND());
				else {
					if (id == -1) {
						instance.friendList = result;
						getFriendList(id);
					} else {
						FriendListPanel.getInstance().load(result, result.getOwner().getUsername()+"的邻友", false);
						FriendsPreviewPanel.getInstance().load(result);
					}
				}
			}
		});
	}

	public void isNeighbour(int id1, int id2, final FriendItem item) {
		service.isNeighbour(id1, id2, new AsyncCallback<Boolean>() {
			@Override
			public void onFailure(Throwable caught) {
				ClientUtility.handleException(caught);
			}

			@Override
			public void onSuccess(Boolean result) {
				if (!result)
					item.addBuildupOperation();
			}
		});
	}

	public void breakup(final int owner, int friend) {
		service.breakup(owner, friend, new AsyncCallback<Void>() {
			@Override
			public void onFailure(Throwable caught) {
				ClientUtility.handleException(caught);
			}

			@Override
			public void onSuccess(Void result) {
				dirty = true;
				getFriendList(owner);
			}
		});
	}

	public void invite(final User user) {
		service.invite(user.getId(), new AsyncCallback<Boolean>() {
			@Override
			public void onFailure(Throwable caught) {
				ClientUtility.handleException(caught);
			}

			@Override
			public void onSuccess(Boolean result) {
				if (result)
					MessageDialogBox.show("已发送邀请", "系统已经向" + user.getUsername()
							+ "发送邀请<br/>" + "对方同意邀请以后,即可成为邻友<br/>");
				else
					MessageDialogBox.show("你们已经是邻友了", "您和" + user.getUsername()
							+ "已经是邻友了哦<br/>" + "请刷新页面<br/>");
			}
		});
	}

	public void findInvite() {
		service.findInvite(new AsyncCallback<UserList>() {
			@Override
			public void onFailure(Throwable caught) {
				ClientUtility.handleException(caught);
			}

			@Override
			public void onSuccess(UserList result) {
				if (result == null)
					Window.Location.replace(Host.getPAGE_NOT_FOUND());
				InvitationListPanel.getInstance().load(result);
			}
		});
	}

	public void buildup(final User user) {
		service.buildup(user.getId(), new AsyncCallback<Boolean>() {
			@Override
			public void onFailure(Throwable caught) {
				ClientUtility.handleException(caught);
			}

			@Override
			public void onSuccess(Boolean result) {
				if (result) {
					dirty = true;
					MessageDialogBox.show("成为好友成功", "您和" + user.getUsername()
							+ "已经成为好友<br/><br/><br/>");
				} else
					MessageDialogBox.show("成为好友失败", "网页过期<br/><br/><br/>");
				findInvite();
			}
		});
	}

	public void cancelInvite(int id) {
		service.cancelInvite(id, new AsyncCallback<Void>() {
			@Override
			public void onFailure(Throwable caught) {
				ClientUtility.handleException(caught);
			}

			@Override
			public void onSuccess(Void result) {
				findInvite();
			}
		});
	}

	public void searchUser(final String userName) {
		service.searchUser(userName, new AsyncCallback<UserList>() {
			@Override
			public void onFailure(Throwable caught) {
				ClientUtility.handleException(caught);
			}

			@Override
			public void onSuccess(UserList result) {
				FriendListPanel.getInstance().load(result, "搜索结果:"+userName, false);
			}
		});
	}

	public void recommend(Set<Integer> fromIds, Set<Integer> toIds) {
		service.recommend(fromIds, toIds, new AsyncCallback<Void>() {
			@Override
			public void onFailure(Throwable caught) {
				ClientUtility.handleException(caught);
			}

			@Override
			public void onSuccess(Void result) {
				MessageDialogBox.show("成功", "已成功推荐好友!");
			}
		});
	}

	public void deleteRecommend(int id){
		service.deleteRecommend(id, new AsyncCallback<Void>(){
			@Override
			public void onFailure(Throwable caught) {
				ClientUtility.handleException(caught);
			}
			@Override
			public void onSuccess(Void result) {
				
			}
		});
	}
	
	public void getRecommendList(){
		service.getRecommendList(new AsyncCallback<UserList>(){
			@Override
			public void onFailure(Throwable caught) {
				ClientUtility.handleException(caught);
			}
			@Override
			public void onSuccess(UserList result) {
				MyRecommendationList.getInstance().load(result);
			}
		});
	}
	
}
